package net.hands.common.mail;

import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataSource;
import javax.mail.util.ByteArrayDataSource;
/**
* 说明:字节数组
 * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class ByteArrayAttachment extends EmailAttachment {
   /**
    * 内容
    */
	protected final byte[] content;
	/**
	 * 输入流
	 */
	protected final InputStream inputStream;
	/**
	 * 内容类型
	 */
	protected final String contentType;
  /**
   * 
  * Constructor Method  
  * @update 2012-9-20 下午6:25:22  
  * @param content  字节数组
  * @param contentType 内容类型
  * @param name   名称
  * @param contentId  内容id
   */
	public ByteArrayAttachment(final byte[] content, final String contentType, final String name,
			final String contentId) {
		super(name, contentId);
		this.content = content;
		this.inputStream = null;
		this.contentType = contentType;
	}
  /**
   * 
  * Constructor Method  
  * @update 2012-9-20 下午6:25:29  
  * @param content 内容
  * @param contentType 内容类型
  * @param name 名称
   */
	public ByteArrayAttachment(final byte[] content, final String contentType, final String name) {
		super(name, null);
		this.content = content;
		this.inputStream = null;
		this.contentType = contentType;
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-20 下午6:26:26  
   * @param inputStream 输入流
   * @param contentType  内容类型
   * @param name  名称
   * @param contentId  内容id
    */
	public ByteArrayAttachment(final InputStream inputStream, final String contentType,
			final String name, final String contentId) {
		super(name, contentId);
		this.content = null;
		this.inputStream = inputStream;
		this.contentType = contentType;
	}
	/**
	 * 
	* Constructor Method  
	* @update 2012-9-20 下午6:26:36  
	* @param inputStream 输入流
	* @param contentType 内容类型
	* @param name 名称
	 */
	public ByteArrayAttachment(final InputStream inputStream, final String contentType,
			final String name) {
		super(name, null);
		this.content = null;
		this.inputStream = inputStream;
		this.contentType = contentType;
	}
   /**
    * 获得字节数组数据源
   (non-Javadoc)  
   * @see net.hands.common.mail.EmailAttachment#getDataSource()  
   * @update 2012-9-20 下午6:27:17
    */
	@Override
	public DataSource getDataSource() {
		if (inputStream != null) {//输入流不为空
			try {
				return new ByteArrayDataSource(inputStream, contentType);
			} catch (final IOException ioex) {
				throw new MailException(ioex);
			}
		}
		if (content != null) {//字节数组不为空
			return new ByteArrayDataSource(content, contentType);
		}
		throw new MailException("No data source", null);
	}
}